
import 'package:flutter/material.dart';
import 'package:flutter_basic_library/bean/pub_bean.dart';
import 'package:flutter_basic_library/flutter_basic_library.dart';
import 'package:flutter_basic_library/net/BaseListEntity.dart';
import 'package:flutter_basic_library/widget/address/city_picker.dart';
import 'package:lpinyin/lpinyin.dart';

typedef OnFinishCall = void Function(List<AddressBean> data);

const String ADDRESS_URL = '/api/v3/common/listsysRegion';

class AddressSelectUtils {

  ///isMustFour
  ///true 必须选择到四级
  ///false随意即可确定返回数据
  ///请重写获取数据的方法
  static void show(BuildContext context,OnFinishCall onFinishCall,List<AddressBean> initialAddress,{bool isMustFour=true}){
    CityPicker.show(
      context: context,
      opacity: 0.5,
      dismissible: true,
      height: 500,
      titleHeight: 50,
      corner: 10,
      backgroundColor: Colors.white,
      paddingLeft: 15,
      titleWidget: Container(
        padding:const EdgeInsets.only(left: 15),
        child:const Text(
          '请选择地址',
          style: TextStyle(
            color: Colors.black54,
            fontSize: 18,
            fontWeight: FontWeight.bold,
          ),
        ),
      ),
      selectText: "请选择",
      closeWidget:!isMustFour?Container(
        margin: const EdgeInsets.only(top: 10),
        child: const Text('确定',style: TextStyle(color: Colors.black87,fontSize: 16),),
      ):const Icon(Icons.close),
      tabHeight: 40,
      showTabIndicator: true,
      tabIndicatorColor: Colors.blueAccent,
      tabIndicatorHeight: 2,
      labelTextSize: 15,
      selectedLabelColor: Colors.blueAccent,
      unselectedLabelColor: Colors.black54,
      itemHeadHeight: 30,
      itemHeadBackgroundColor: Colors.white,
      itemHeadLineColor: Colors.black,
      itemHeadLineHeight: 0.1,
      itemHeadTextStyle:const TextStyle(fontSize: 15, color: Colors.black),
      itemHeight: 40,
      indexBarWidth: 28,
      indexBarItemHeight: 20,
      indexBarBackgroundColor: Colors.black12,
      indexBarTextStyle:const TextStyle(fontSize: 14, color: Colors.black54),
      itemSelectedIconWidget:
      Icon(Icons.done, color: Colors.blueAccent, size: 16),
      itemSelectedTextStyle: TextStyle(
          fontSize: 14,
          fontWeight: FontWeight.bold,
          color:  Colors.blueAccent),
      itemUnSelectedTextStyle:const TextStyle(fontSize: 14, color: Colors.black54),
      initialAddress: initialAddress,
      cityPickerListener: CityPickerListenerImpl(isMustFour,onFinishCall),
    );
  }

}

class CityPickerListenerImpl implements CityPickerListener{

  bool isMustFour;
  OnFinishCall onFinishCall;

  CityPickerListenerImpl(this.isMustFour,this.onFinishCall);

  @override
  Future<List<AddressBean>> onDataLoad(int index, AddressBean addressNode) async{
    if(index==0){
      await Future.delayed(const Duration(milliseconds: 200));
      return getAddressData(0);
    }else{
      return getAddressData(addressNode.id!);
    }
  }

  @override
  void onFinish(List<AddressBean> data) {
    if(!isMustFour){
      onFinishCall.call(data);
    }else{
      if(data.length==4){
        onFinishCall.call(data);
      }
    }
  }

  Future<List<AddressBean>> getAddressData(num id) async{
    // BaseListEntity<AddressBean> reslut =await NetUtils.instance.requestList2<AddressBean>((json) => AddressBean.fromJson(json), ADDRESS_URL,params: {
    //   'parentId':id
    // });
    // for (var element in reslut.data!) {
    //   element.letter = PinyinHelper.getFirstWordPinyin(element.name!)
    //       .substring(0, 1)
    //       .toUpperCase();
    // }
    return [];
  }

}